﻿using Domain.InvoiceTicket.Ticket;
using Domain.Production_Modules;
using Infratructure.Interface;
using MediatR;
using MES.Server.API.Application.Command.InvoiceTicket.Ticket;
using MES.Server.API.Dto.InvoiceTicketDTO;
using ReturnValue;

namespace MES.Server.API.Application.Handler.InvoiceTicket.Ticket
{
    public class TicketQueryCommandHandler : IRequestHandler<TicketQueryCommand, ApiResult<List<GetTicketDto>>>
    {
        IRepository<TicketModel> ticketRep;
        IRepository<OrderModel> orderRep;
        IRepository<TicketorderModel> torep;
        
        public TicketQueryCommandHandler(IRepository<TicketModel> ticketRep, IRepository<OrderModel> orderRep, IRepository<TicketorderModel> torep)
        {
            this.ticketRep = ticketRep;
            this.orderRep = orderRep;
            this.torep = torep;
        }
        public Task<ApiResult<List<GetTicketDto>>> Handle(TicketQueryCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<GetTicketDto>> result = new ApiResult<List<GetTicketDto>>();
            result.Code=StatusCode.Success;
            result.Message = StatusCode.Success.ToString();
            var list = from a in ticketRep.GetAll()
                       join b in torep.GetAll() on a.Id equals b.TicketId
                       join c in orderRep.GetAll() on b.OrderId equals c.Id
                       where !a.IsDel
                       select new GetTicketDto
                       {
                           TicketCode = a.TicketCode,
                           TicketName = a.TicketName, 
                           TicketType = a.TicketType,
                           TicketDate = a.TicketDate,
                           TicketMomeny = a.TicketMomeny,
                           ReceiveTicket = a.ReceiveTicket,
                           TicketDesc = a.TicketDesc,
                           Id = a.Id,

                       };
            result.Data=list.ToList();
            return Task.FromResult(result);
        }
    }
}
